package cn.signalpha.modules.schedule.service;

import cn.signalpha.core.tool.utils.DateUtil;
import cn.signalpha.modules.schedule.entity.ScheduleJob;
import cn.signalpha.modules.schedule.mapper.ScheduleJobMapper;
import cn.signalpha.modules.schedule.utils.Constant;
import cn.signalpha.modules.schedule.utils.ScheduleUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
@AllArgsConstructor
public class ScheduleJobService extends ServiceImpl<ScheduleJobMapper, ScheduleJob> {

    private final Scheduler scheduler;

	/**
	 * 项目启动时，初始化定时器
	 */
	@PostConstruct
	public void init(){
		List<ScheduleJob> scheduleJobList = this.list();
		for(ScheduleJob scheduleJob : scheduleJobList){
			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
            //如果不存在，则创建
            if(cronTrigger == null) {
                ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
            }else {
                ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
            }
		}
	}

	/**
	 * 保存定时任务
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean saveJob(ScheduleJob scheduleJob) {
		scheduleJob.setCreateTime(DateUtil.now());
		scheduleJob.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
        this.save(scheduleJob);
        ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
        return true;
    }

	/**
	 * 更新定时任务
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean update(ScheduleJob scheduleJob) {
        ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
        this.updateById(scheduleJob);
		return true;
    }

	/**
	 * 批量删除定时任务
	 */
	@Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(Integer[] jobIds) {
    	for(Integer jobId : jobIds){
    		ScheduleUtils.deleteScheduleJob(scheduler, jobId);
    	}
    	//删除数据
    	this.removeByIds(Arrays.asList(jobIds));
    	return true;
	}

	/**
	 * 立即执行
	 */
	@Transactional(rollbackFor = Exception.class)
    public void run(Integer[] jobIds) {
    	for(Integer jobId : jobIds){
    		ScheduleUtils.run(scheduler, this.getById(jobId));
    	}
    }

	/**
	 * 暂停运行
	 */
	@Transactional(rollbackFor = Exception.class)
    public void pause(Integer[] jobIds) {
        for(Integer jobId : jobIds){
    		ScheduleUtils.pauseJob(scheduler, jobId);
    		updateById(ScheduleJob.builder().jobId(jobId).status(Constant.ScheduleStatus.PAUSE.getValue()).build());
    	}
    }

	/**
	 * 恢复运行
	 */
	@Transactional(rollbackFor = Exception.class)
    public void resume(Integer[] jobIds) {
    	for(Integer jobId : jobIds){
    		ScheduleUtils.resumeJob(scheduler, jobId);
			updateById(ScheduleJob.builder().jobId(jobId).status(Constant.ScheduleStatus.NORMAL.getValue()).build());
    	}
    }

}
